package Test;

import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Test;

import DTO.Field;
import DTO.ForeignKey;
import DTO.Row;
import XML.XMLHelper;
public class Database_Test {

	XMLHelper xmlHelper = new XMLHelper();

	
	/**
	/* * Database's name is invalid
	 */
	@Test
	public final void testCreateDatabase1() {
		
		boolean actual = xmlHelper.CreateDatabase("");
		boolean expected = false;
		Assert.assertEquals(expected, actual);
	}
	
	
	@Test
	public final void testCreateDatabase2() {
		if(xmlHelper.IsExist("HOCSINH1")) {
			xmlHelper.DropDatabase("HOCSINH1");
		}
		boolean actual = xmlHelper.CreateDatabase("HOCSINH1");
		boolean expected = true;
		Assert.assertEquals(expected, actual);
	}
	
	/* 
	* End test method create data base	 
	*/
	
	
	@Test
	public final void testDropDatabase()
	{
		String dbName ="DB"; 
		boolean expected = true;
		if(!xmlHelper.IsExist(dbName)) expected = false;
		boolean actual = xmlHelper.DropDatabase(dbName);
		Assert.assertEquals(expected, actual);
	}
	
	/**
	 * The database's name is invalid.
	 */
	@Test
	public final void testDropDatabase1()
	{
		boolean actual = xmlHelper.DropDatabase("");
		boolean expected = false;
		Assert.assertEquals(expected, actual);
	}
	
	
	@Test
	public final void testRenameDatabase()
	{
		String scrDbName = "DB1";
		String destDbName = "DB2";
		if(!xmlHelper.IsExist(scrDbName) || xmlHelper.IsExist(destDbName))
		{
			boolean expected = false;
			boolean actual = xmlHelper.RenameDatabase(scrDbName, destDbName);
			Assert.assertEquals(expected, actual);
		}
		else{
		boolean actual = xmlHelper.RenameDatabase(scrDbName, destDbName);
		boolean expected = true;
		Assert.assertEquals(expected, actual);
		}	
	}
	
	
	/**
	 * The source database's name is not exist.
	 */
	@Test
	public final void testRenameDatabase3()
	{
		boolean actual = xmlHelper.RenameDatabase("A", "DB_Test");
		boolean expected = false;
		Assert.assertEquals(expected, actual);
	}
	
	@Test
	public final void testCreateTableStructure()
	{
		String databaseName = "HOCSINH";
		String tableName = "HocSinh";
		Vector<Field> fields = new Vector<Field>();
		ForeignKey foreignKey = new ForeignKey("", "", "");
		
		fields.add(new Field("MaHS", true,
				0, true,foreignKey));
		fields.add(new Field("TenHS", false,
				1, false, foreignKey));
		if(!xmlHelper.IsExist(databaseName)) {
			boolean expect = false;
			boolean actual = xmlHelper.CreateTableStructure(databaseName, tableName, fields);
			Assert.assertEquals(expect, actual);
		}
		
		else{
		boolean actual = xmlHelper.CreateTableStructure(databaseName, tableName, fields);
		boolean expected = true;
		Assert.assertEquals(expected, actual);
		}
	}
	
	@Test
	public final void testCreateTableStructure1()
	{
		String databaseName = "HOCSINH";
		String tableName = "Diem";
		String destTbName = "HocSinh";
		
		Vector<Field> fields = new Vector<Field>();
		ForeignKey foreignKey = new ForeignKey("", "", "");
		
		fields.add(new Field("MaHS", true,
				0, true,foreignKey));
		fields.add(new Field("Diem", false,
				0, false, new ForeignKey("HocSinh", "MaHS", "MaHS")));
		boolean expected = true;
		if(!xmlHelper.IsExist(databaseName, destTbName))  {
			expected = false;
		} 
		boolean actual = xmlHelper.CreateTableStructure(databaseName, tableName, fields);
		
		Assert.assertEquals(expected, actual);
	}

	@Test
	public final void testInsertField()
	{
		String dbName = "HOCSINH";
		String tbName = "HocSinh";
		
		
		ForeignKey f = new ForeignKey("", "", "");
		Field field = new Field("Quequan", false,
			1, false, f);
		if(!xmlHelper.IsExist(dbName, tbName)) 
		{
			boolean expected = false;
			boolean actual =  xmlHelper.InsertField(dbName, tbName, field);
			Assert.assertEquals(expected, actual);
		}
		else{
		boolean actual = xmlHelper.InsertField(dbName, tbName, field);
		boolean expected = true;
		Assert.assertEquals(expected, actual);
		}
	}
	

	@Test
	public final void testGetListFieldName()
	{
		String dbName = "HOCSINH";
		String tbName = "HocSinh";
		if(!xmlHelper.IsExist(dbName, tbName)) 
		{
			Vector<String> expected = new Vector<String>();  expected = null;
			Vector<String> actual =  xmlHelper.GetListOfFieldName(dbName, tbName);
			Assert.assertEquals(expected, actual);
		}
		else{
		Vector<String> actual = xmlHelper.GetListOfFieldName(dbName, tbName);
		Vector<String> expected = new Vector<String>();
		expected.add("MaHS");
		expected.add("TenHS");
		expected.add("Quequan");
		
		Assert.assertEquals(expected, actual);
		}
	}
	
	@Test
	public final void testGetListField()
	{
		String dbName = "HOCSINH";
		String tbName = "HocSinh";
		if(!xmlHelper.IsExist(dbName, tbName)) 
		{
			Vector<Field> f1 = new Vector<Field>(); f1 = null;
			Vector<Field> f =   xmlHelper.GetListOfField(dbName, tbName);
			Assert.assertEquals(f, f1);
		}
		else{
		Vector<Field> actual = xmlHelper.GetListOfField(dbName, tbName);
		Vector<Field> expected = new Vector<Field>();
		
		ForeignKey foreignKey = new ForeignKey("", "", "");
		Field f1 = new Field("MaHS", true,
				0, true,foreignKey);
		Field f2 = new Field("TenHS", false,
				1, false, foreignKey);
		Field f3 = new Field("Quequan", false,
				1, false, foreignKey);
		expected.add(f1);
		expected.add(f2);
		expected.add(f3);
		int n = actual.size();
		int d = 0;
		for(int i = 0 ; i < n; i++)
			if(expected.get(i).Compare(actual.get(i)))
				d++;
		Assert.assertEquals(d, n);
		}
			
	}
	
	@Test
	public final void testInsertRow()
	{
		String dbName = "HOCSINH";
		String tbName = "HocSinh";
		
		Row row;
		Vector<String> fields = new Vector<String>();
		fields.add("MaHS");
		fields.add("TenHS");
		fields.add("Quequan");
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(fields.get(0), new Integer(325));
		map.put(fields.get(1), new String("Nguyen A"));
		map.put(fields.get(2), new String("Quang Nam"));
		row = new Row(map, false);
		if(!xmlHelper.IsExist(dbName, tbName)) 
		{
			boolean expected = false;
			boolean actual =  xmlHelper.InsertRow(dbName, tbName, row);
			Assert.assertEquals(expected, actual);
		}
		else{
		
		boolean actual = xmlHelper.InsertRow(dbName, tbName, row);
		boolean expected = false;
		
		Assert.assertEquals(expected, actual);
		}
	}
	
	@Test
	public final void testInsertRow11()
	{
		String dbName = "HOCSINH";
		String tbName = "HocSinh";
		
		Row row;
		Vector<String> fields = new Vector<String>();
		fields.add("MaHS");
		fields.add("TenHS");
		fields.add("Quequan");
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(fields.get(0), new Integer(326));
		map.put(fields.get(1), new String("Nguyen B"));
		map.put(fields.get(2), new String("Ho Chi Minh"));
		row = new Row(map, false);
		if(!xmlHelper.IsExist(dbName, tbName)) 
		{
			boolean expected = false;
			boolean actual =   xmlHelper.InsertRow(dbName, tbName, row);
			Assert.assertEquals(expected, actual);
		}
		else{		
		boolean actual = xmlHelper.InsertRow(dbName, tbName, row);
		boolean expected = false;
		
		Assert.assertEquals(expected, actual);
		}
	}
	//existed
	@Test
	public final void testInsertRow2()
	{
		String dbName = "HOCSINH";
		String tbName = "HocSinh";
		Row row;
		Vector<String> fields = new Vector<String>();
		fields.add("MaHS");
		fields.add("TenHS");
		fields.add("Quequan");
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(fields.get(0), new Integer(325));
		map.put(fields.get(1), new String("Nguyen A"));
		map.put(fields.get(2), new String("Quang Nam"));
				
		row = new Row(map, false);
		if(!xmlHelper.IsExist(dbName, tbName)) 
		{
			boolean expected = false;
			boolean actual =   xmlHelper.InsertRow(dbName, tbName, row);
			Assert.assertEquals(expected, actual);
		}
		else{	
		boolean actual = xmlHelper.InsertRow(dbName, tbName, row);
		boolean expected = false;
		
		Assert.assertEquals(expected, actual);
		}
	}
	
	@Test
	public final void testGetListOfRow()
	{
		String dbName = "HOCSINH";
		String tbName = "HocSinh";
		if(!xmlHelper.IsExist(dbName, tbName + "data")) 
		{
			Vector<Row> expected= new Vector<Row>(); expected = null;
			Vector<Row> actual =  xmlHelper.GetListOfRow(dbName, tbName);
			Assert.assertEquals(expected, actual);
		}
		else{	
		Vector<Row> actual = xmlHelper.GetListOfRow(dbName, tbName);
		int n = actual.size();
		Assert.assertEquals(2, n);
		}
	}
	

	@Test
	public final void testGetPrimaryKey()
	{
		String dbName = "HOCSINH";
		String tbName = "HocSinh";
		ForeignKey foreignKey = new ForeignKey("", "", "");
		Field f = new Field("MaHS", true,0, true,foreignKey);
		if(!xmlHelper.IsExist(dbName, tbName)) 
		{
			Field expected= null;
			Field actual =  xmlHelper.getPrimaryKey(dbName, tbName);
			Assert.assertEquals(expected, actual);
		}
		else{
		Field actual = xmlHelper.getPrimaryKey(dbName, tbName);
		boolean ex = false;
		if(f.Compare(actual)){ ex = true;}
		
		Assert.assertEquals(ex, true);
		}
	}
	
	@Test
	public final void testDeleteRow()
	{
		String dbName = "HOCSINH";
		String tbName = "HocSinh";
		
		
		
		if(!xmlHelper.IsExist(dbName, tbName + "data")) 
		{
			boolean expected= false;
			boolean actual = xmlHelper.DeleteRow(dbName, tbName, 325);
			Assert.assertEquals(expected, actual);
		}
		else 
		{
			Vector<Row> delRows = xmlHelper.GetDeletedRow(dbName, tbName);
			int n = delRows.size();
			if(n > 0)
			{
				boolean expected = false;
				Vector<Row> rows = xmlHelper.GetRows(dbName, tbName, "MaHS", 325);
				//if(rows.size() == 0)
				int index = xmlHelper.IsExist(delRows, rows.get(0));
				
				boolean actual = xmlHelper.DeleteRow(dbName, tbName, 325);
				Assert.assertEquals(expected, actual);
			}
			else if(n == 0)
			{			
				boolean actual = xmlHelper.DeleteRow(dbName, tbName, 325);
				boolean expected = true;
				Assert.assertEquals(expected, actual);
			}
		}
		
	}
	

	@Test
	public final void testDeleteRow1()
	{
		String dbName = "HOCSINH";
		String tbName = "HocSinh";
		if(!xmlHelper.IsExist(dbName, tbName + "data")) 
		{
			boolean expected= false;
			boolean actual = xmlHelper.DeleteRow(dbName, tbName, 331);
			Assert.assertEquals(expected, actual);
		}
		else 
		{
		boolean actual = xmlHelper.DeleteRow(dbName, tbName, 331);
		boolean expected = false;
		Assert.assertEquals(expected, actual);
		}
	}
	
	@Test
	public final void testInsertRow1()
	{
		String dbName = "HOCSINH";
		String tbName = "HocSinh";
		Row row;
		Vector<String> fields = new Vector<String>();
		fields.add("MaHS");
		fields.add("TenHS");
		fields.add("Quequan");
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(fields.get(0), new Integer(325));
		map.put(fields.get(1), new String("Nguyen A"));
		map.put(fields.get(2), new String("Quang Nam"));
		row = new Row(map, false);
		if(!xmlHelper.IsExist(dbName, tbName + "data")) 
		{
			boolean expected= false;
			boolean actual = xmlHelper.InsertRow(dbName, tbName, row);
			Assert.assertEquals(expected, actual);
		}
		else 
		{		
		boolean actual = xmlHelper.InsertRow(dbName, tbName, row);
		boolean expected = true;
		
		Assert.assertEquals(expected, actual);
		}
	}

	@Test
	public final void testGetRows()
	{
		String dbName = "HOCSINH";
		String tbName = "HocSinh";
		if(!xmlHelper.IsExist(dbName, tbName + "data")) 
		{
			Vector<Row> expected= new Vector<Row>();
			Vector<Row> actual = xmlHelper.GetRows(dbName, tbName, "Quequan", "Quang Nam");;
			Assert.assertEquals(expected, actual);
		}
		else 
		{	
		Vector<Row> rows = xmlHelper.GetRows(dbName, tbName, "Quequan", "Quang Nam");
		
		int n = rows.size();
		int expected = 0;
		Assert.assertEquals(n, expected);
		}
	}
	
	
	@Test
	public final void testUpdateRow()
	{
		String dbName = "HOCSINH";
		String tbName = "NhanVien";
		
		Vector<String> fieldUpdate = new Vector<String>();
		fieldUpdate.add("Quequan");
		
		Vector<Object> valUpdate = new Vector<Object>();
		valUpdate.add(new String("Quang Nam"));
		
		Vector<String> fieldCond = new Vector<String>();
		fieldCond.add("Quequan");
		
		Vector<Object> valCond = new Vector<Object>();
		valCond.add("Ho Chi Minh");
		
		Vector<Integer>valType = new Vector<Integer>();
		valType.add(1);
		
		Vector<Row> rows = xmlHelper.GetAllRows(dbName, tbName);
		
		if(!xmlHelper.IsExist(dbName, tbName + "data")) 
		{
			boolean expected= false;
			boolean actual = xmlHelper.UpdateRow(dbName, tbName, fieldUpdate, valUpdate,valType, rows);
			Assert.assertEquals(expected, actual);
		}
		else 
		{	
		boolean actual = xmlHelper.UpdateRow(dbName, tbName, fieldUpdate, valUpdate,valType, rows);
		boolean expected = true;
		Assert.assertEquals(expected, actual);
		}
	}
	

	String dbName = "HOCSINH";
	String tbName = "HocSinh";
	@Test
	public final void testGetDeletedRow() {
		
		if(!xmlHelper.IsExist(dbName, tbName + "data")) 
		{
			Vector<Row> expected= new Vector<Row>(); expected = null;
			Vector<Row> actual = xmlHelper.GetDeletedRow(dbName, tbName);
			Assert.assertEquals(expected, actual);
		}
		else 
		{	
		Vector<Row> rows = xmlHelper.GetDeletedRow(dbName, tbName);
		int expected = 0;
		int actual = rows.size();
		Assert.assertEquals(expected, actual);
		}
	}
	@Test
	public final void testRecoveryRow() {
	
		if(!xmlHelper.IsExist(dbName, tbName + "data")) 
		{
			boolean expected = false;
			
			Assert.assertEquals(expected, false);
		}
		else 
		{	
		Vector<Row> rows = xmlHelper.GetDeletedRow(dbName, tbName);
		int  n = rows.size();
		if(n == 0 )
		{
			boolean actual = false;
			boolean expected = false;
			Assert.assertEquals(expected, actual);
		}
		else
		{
		Row row = rows.get(0);
		boolean actual = xmlHelper.RecoveryRow(dbName, tbName, row);
		boolean expected = true;
		Assert.assertEquals(expected, actual);
		}
		}
	}


}
